#include "PlayerControl.h"
//#include <SFML/Window/Input.hpp>

PlayerControl::PlayerControl(const sf::Window &win):w(win){}

bool PlayerControl::ControlPressed(Controls c){
    return (controlStatus&c)==c;
}


KeyboardControl::KeyboardControl(const sf::Window &w, sf::Key::Code up, sf::Key::Code down, sf::Key::Code left, sf::Key::Code right, sf::Key::Code button1, sf::Key::Code button2):PlayerControl(w){
    this->up=up;
    this->down=down;
    this->left=left;
    this->right=right;
    this->button1=button1;
    this->button2=button2;
}


void KeyboardControl::Update(){
    controlStatus=0;
    const sf::Input &i=w.GetInput();
    if(i.IsKeyDown(up)) controlStatus|=CONTROL_UP;
    if(i.IsKeyDown(down)) controlStatus|=CONTROL_DOWN;
    if(i.IsKeyDown(left)) controlStatus|=CONTROL_LEFT;
    if(i.IsKeyDown(right)) controlStatus|=CONTROL_RIGHT;
    if(i.IsKeyDown(button1)) controlStatus|=CONTROL_BUTTON1;
    if(i.IsKeyDown(button2)) controlStatus|=CONTROL_BUTTON2;
}



JoystickControl::JoystickControl(const sf::Window &w, unsigned int joyId):PlayerControl(w){
    this->joyId=joyId;
    // TODO: no se como saber si el joystick esta conectado
//    if(!sf::Joystick::IsConnected(joyId)){
//        printf("ERROR: El joystick %i no esta conectado.\n",%joyId)
//    }

}

void JoystickControl::Update(){
    controlStatus=0;
    const sf::Input &i=w.GetInput();
    if(i.GetJoystickAxis(joyId,sf::Joy::AxisY)<0) controlStatus|=CONTROL_UP;
    if(i.GetJoystickAxis(joyId,sf::Joy::AxisY)>0) controlStatus|=CONTROL_DOWN;
    if(i.GetJoystickAxis(joyId,sf::Joy::AxisX)<0) controlStatus|=CONTROL_LEFT;
    if(i.GetJoystickAxis(joyId,sf::Joy::AxisX)>0) controlStatus|=CONTROL_RIGHT;
    if(i.IsJoystickButtonDown(joyId, 0)) controlStatus|=CONTROL_BUTTON1;
    if(i.IsJoystickButtonDown(joyId, 1)) controlStatus|=CONTROL_BUTTON2;
}
